package com.pearl.boot.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * 枚举校验器
 *
 * @author TangDan
 * @version 1.0
 * @since 2022/10/27
 */
public class EnumValidator implements ConstraintValidator<EnumValid, String> {


    private Class<? extends Enum> enumClass;


    @Override
    public void initialize(EnumValid actionTypeEnumValid) {
        enumClass = actionTypeEnumValid.enumClass();
    }


    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null || "".equals(value)) {
            return true;
        }
        EnumValidate[] enums = (EnumValidate[]) enumClass.getEnumConstants();
        if (enums == null || enums.length == 0) {
            return false;
        }


        return enums[0].exist(value);
    }

}
